/*
 * File: javaScriptMinifier.java Author: Robert Bittle <guywithnose@gmail.com>
 */
package fileHandlers;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import com.google.javascript.jscomp.CommandLineRunner;
import com.google.javascript.jscomp.Compiler;

/**
 * The Class javaScriptMinifier.
 * 
 * @author Dude
 */
public class JavaScriptMinifier extends CommandLineRunner
{    

    @Override
    protected Compiler createCompiler() {
        Compiler c = new Compiler(getErrorPrintStream());
        c.disableThreads();
        return c;
    }

    /**
     * Instantiates a new java script minifier.
     * 
     * @param args
     *            the args
     * @param out
     *            the out
     * @param err
     *            the err
     */
    protected JavaScriptMinifier(String[] args, PrintStream out, PrintStream err)
    {
        super(args, out, err);
        try
        {
            doRun();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     * Minify.
     * 
     * @param fileName
     *            the file name
     * @return the string
     */
    @SuppressWarnings("unused")
    public static String minify(String fileName)
    {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        JavaScriptMinifier unused = new JavaScriptMinifier(new String[] { "--js", fileName }, new PrintStream(out),
                System.err);
        return out.toString();
    }

}
